package org.adream.account.rest.api;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.adream.account.entity.CategoryEntity;
import org.adream.account.model.CategoryVo;
import org.adream.account.model.ResultModel;
import org.adream.account.service.ADreamServiceException;
import org.adream.account.service.CategoryService;
import org.adream.account.util.Utils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageInfo;

/**
 * 类别:行业类别、机构类别
 * @author sl
 *
 */
@Controller
@RequestMapping("/category")
public class CategoryApiController {

	@Autowired
	private CategoryService categoryService;

	private final static Logger logger = LoggerFactory.getLogger(CategoryApiController.class);

	
	@RequestMapping(value = "", produces = "application/json;charset=UTF-8", method = RequestMethod.POST)
	@ResponseBody
	public ResultModel<String> addCategory(@RequestBody CategoryEntity categoryEntity) {
		if(categoryEntity == null || StringUtils.isEmpty(categoryEntity.getTitle())) {
			return new ResultModel<String>(ResultModel.ERR_PARAM, "新增失败,请求参数有误", null);
		}
		return categoryService.addCategory(categoryEntity);
	}

	@RequestMapping(value = "/{categoryId}", produces = "application/json;charset=UTF-8", method = RequestMethod.DELETE)
	@ResponseBody
	public ResultModel<String> deleteCategory(@PathVariable int categoryId, HttpServletRequest request) {
		return categoryService.deleteCategoryById(categoryId);
	}

	//删除多个类别
	@RequestMapping(value = "/categoryIds", produces = "application/json;charset=UTF-8", method = RequestMethod.DELETE)
	@ResponseBody
	public ResultModel<String> deleteCategoryByIds(@RequestBody int[] categroyIds) {
		if(categroyIds == null || categroyIds.length == 0) {
			return new ResultModel<String>(ResultModel.ERR_PARAM, "删除失败,请求参数有误", null);
		}
		for (int id : categroyIds) {
			categoryService.deleteCategoryById(id);
		}
		return new ResultModel<String>(ResultModel.SUCCESS, ResultModel.SUCCESS_MSG, null);
	}

	@RequestMapping(value = "/pid/{pid}", produces = "application/json;charset=UTF-8", method = RequestMethod.DELETE)
	@ResponseBody
	public ResultModel<String> deleteCategoryByPid(@PathVariable int pid) {
		try {
			return categoryService.deleteCategoryByPid(pid);
		} catch (ADreamServiceException e) {
			logger.warn("删除列表失败,{}", e.getMessage());
			return new ResultModel<String>(ResultModel.ERR_SYS, e.getMessage(), null);
		}
	}

	//更新类别
	@RequestMapping(value = "/{categoryId}", produces = "application/json;charset=UTF-8", method = RequestMethod.PUT)
	@ResponseBody
	public ResultModel<String> updateCategory(@PathVariable int categoryId, @RequestBody CategoryEntity categoryEntity) {
		if(categoryEntity == null || StringUtils.isEmpty(categoryEntity.getTitle())) {
			return new ResultModel<String>(ResultModel.ERR_PARAM, "更新失败,请求参数有误", null);
		}
		return categoryService.updateCategoryById(categoryId, categoryEntity);
	}

	@RequestMapping(value = "/{categoryId}", produces = "application/json;charset=UTF-8", method = RequestMethod.GET)
	@ResponseBody
	public ResultModel<CategoryEntity> queryCategoryById(@PathVariable int categoryId) {
		CategoryEntity category = categoryService.queryCategoryById(categoryId);
		if(category == null) {
			return new ResultModel<CategoryEntity>(ResultModel.NOT_FOUND_OBJECT, "查询失败,数据不存在", null);
		}
		return new ResultModel<CategoryEntity>(ResultModel.SUCCESS, ResultModel.SUCCESS_MSG, category);
	}

	//查询所有类别
	@RequestMapping(value = "/allcategory", produces = "application/json;charset=UTF-8", method = RequestMethod.GET)
	@ResponseBody
	public ResultModel<?> queryCategory() {
		List<CategoryEntity> list = categoryService.queryCategory();
		return new ResultModel<List<CategoryEntity>>(ResultModel.SUCCESS, ResultModel.SUCCESS_MSG, list);
	}

	@RequestMapping(value = "/pid/{pid}", produces = "application/json;charset=UTF-8", method = RequestMethod.GET)
	@ResponseBody
	public ResultModel<List<CategoryEntity>> queryCategoryByPid(@PathVariable int pid) {
		List<CategoryEntity> categories = categoryService.queryCategoryByPid(pid);
		if(Utils.isEmptyCollection(categories)) {
			return new ResultModel<List<CategoryEntity>>(ResultModel.NOT_FOUND_OBJECT, "查询失败,数据不存在", null);
		}
		return new ResultModel<List<CategoryEntity>>(ResultModel.SUCCESS, ResultModel.SUCCESS_MSG, categories);
	}

	@RequestMapping(value = "/dr/{dr}", produces = "application/json;charset=UTF-8", method = RequestMethod.GET)
	@ResponseBody
	public ResultModel<?> queryCategoryByDr(@PathVariable int dr) {
		try {
			List<CategoryEntity> list = categoryService.queryCategoryByDr(dr);
			return new ResultModel<List<CategoryEntity>>(ResultModel.SUCCESS, ResultModel.SUCCESS_MSG, list);
		} catch (ADreamServiceException e) {
			return new ResultModel<String>(ResultModel.ERR_PARAM, e.getLocalizedMessage(), null);
		}
	}

	//检查类别名称是否有效
	@RequestMapping(value = "/checkCategoryTitle", method = RequestMethod.GET)
	@ResponseBody
	public void isValidCategoryTitle(String title, HttpServletResponse response) {
		boolean isValid = categoryService.isValidCategoryTitle(title);
		PrintWriter out = null;
		try {
			out = response.getWriter();
			out.write(String.valueOf(isValid));
		} catch (IOException e) {
			logger.info("检查resName出错:)");
		} finally {
			if (out != null) {
				out.close();
			}
		}
	}

	//关键字查询类别
	@RequestMapping(value = "/keyword",produces = "application/json;charset=UTF-8", method = RequestMethod.GET)
	@ResponseBody
	public ResultModel<?> queryCategoryByKeywordAndPid(String keyword,String pid, Integer pageNo, Integer pageSize) {
		PageInfo<CategoryVo> page = categoryService.queryCategoryByKeywordAndPid(keyword,pid,pageNo,pageSize);
		return new ResultModel<PageInfo<CategoryVo>>(ResultModel.SUCCESS, ResultModel.SUCCESS_MSG, page);
	}
}